package lyc.springcloud.admin.controller.systemManagerController;

import lombok.extern.slf4j.Slf4j;
import lyc.springcloud.admin.dto.systemManagerDto.ResourceManagerDto;
import lyc.springcloud.admin.service.def.systemManagerService.ResourceManagerService;
import lyc.springcloud.admin.utils.CommonResClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/systemManager/resourceManager")
@Slf4j
public class ResourceManagerController {
    @Autowired
    private ResourceManagerService resourceManagerService;

    /**
     * 资源管理---查询所有资源tree（包括菜单，不包括功能）
     * */
    @PostMapping("selectResourceTree")
    public CommonResClass selectResourceTree(){
        try {
            Map<String, Object> map =  resourceManagerService.selectResourceTree();
            return CommonResClass.success(map);
        } catch (Exception e) {
            log.error("查询所有资源菜单", e);
            return CommonResClass.failure();
        }
    }

    /**
     * 资源管理---根据resourceId查询下一级子节点
     * */
    @PostMapping("selectChildByResourceId")
    public CommonResClass selectChildByResourceId(@RequestBody ResourceManagerDto params){
        try {
            Map<String, Object> map =  resourceManagerService.selectChildByResourceId(params);
            return CommonResClass.success(map);
        } catch (Exception e) {
            log.error("根据resourceId查询下一级子节点", e);
            return CommonResClass.failure();
        }
    }

    /**
     * 资源管理---新增
     * */
    @PostMapping("insert")
    public CommonResClass resourceMng_insert(@RequestBody ResourceManagerDto params){
        try {
            resourceManagerService.resourceMng_insert(params);
            return CommonResClass.success();
        } catch (Exception e) {
            log.error("资源管理---新增", e);
            return CommonResClass.failure();
        }
    }

    /**
     * 资源管理---编辑
     * */
    @PostMapping("update")
    public CommonResClass resourceMng_update(@RequestBody ResourceManagerDto params){
        try {
            resourceManagerService.resourceMng_update(params);
            return CommonResClass.success();
        } catch (Exception e) {
            log.error("资源管理---编辑", e);
            return CommonResClass.failure();
        }
    }

    /**
     * 资源管理---删除
     * */
    @GetMapping("delete")
    public CommonResClass resourceMng_delete(@RequestParam("resourceIds") String params){
        try {
            resourceManagerService.resourceMng_delete(params);
            return CommonResClass.success();
        } catch (Exception e) {
            log.error("资源管理---删除", e);
            return CommonResClass.failure();
        }
    }
}
